﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Calendar;
using SmartHealthCareDbLayer;

namespace SmartHealthCare.Frm
{
    public partial class FrmConsultaMedica : Form
    {
        public delegate void AddItemConsulta(ref CalendarItem item);

        public FrmConsultaMedica()
        {
            InitializeComponent();
        }

        private List<CalendarItem> lista = new List<CalendarItem>();
        private void FrmConsultaMedica_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            _cbMedico.ComboBox.DataSource = ModelSingle.Instance.Medicos.ToList();
            _cbMedico.ComboBox.DisplayMember = "Nombre";
            _cbMedico.ComboBox.ValueMember = "IdMedico";
            _cbMedico.ComboBox.SelectedValueChanged += ComboBox_SelectedValueChanged;
        }

        void ComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            var medico = ((ComboBox)sender).SelectedItem as Medicos;
            if (medico == null) return;

            var temp = (from c in ModelSingle.Instance.ConsultaMedica
                        where c.IdMedico == medico.IdMedico
                        select c).ToList();

            lista = new List<CalendarItem>();

            foreach (var item in temp)
            {
                lista.Add(new CalendarItem(this._calendar, item.FechaCita.Value, item.FechaCita.Value, item.Nota) { Tag = item });
            }
            LoadItems(lista);


        }

        private void MonthSelectionChanged(object sender, EventArgs e)
        {
            var temp = (_month.SelectionEnd - _month.SelectionStart).Days;
            if (temp < _calendar.MaximumViewDays)
            {
                _calendar.SetViewRange(_month.SelectionStart, _month.SelectionEnd);
                LoadItems(lista);
            }
        }

        private void CalendarDayHeaderClick(object sender, CalendarDayEventArgs e)
        {
            _calendar.SetViewRange(e.CalendarDay.Date, e.CalendarDay.Date);
            LoadItems(lista);
        }

        private void CalendarItemCreated(object sender, CalendarItemCancelEventArgs e)
        {
            var temp = e.Item;
            var frm = new FrmConsultaMedicaItem(ref temp, addItemConsulta);
            frm.ShowDialog(this);
        }

        private void addItemConsulta(ref CalendarItem item)
        {
            if (lista.Contains(item)) { lista.Remove(item); }
            //
            lista.Add(item);
            _calendar.Invalidate(item);
        }



        void LoadItems(IEnumerable<CalendarItem> list)
        {
            foreach (var calendarItem in list)
            {
                if (_calendar.ViewIntersects(calendarItem))
                {
                    _calendar.Items.Add(calendarItem);
                }
            }
        }

        private void CalendarItemDoubleClick(object sender, CalendarItemEventArgs e)
        {

            _calendar.ActivateEditMode();
            var temp = e.Item;
            var frm = new FrmConsultaMedicaItem(ref temp, addItemConsulta);
            frm.ShowDialog(this);
        }
    }
}
